package pl.edu.utp.kursy;

import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Set;
import javax.swing.DefaultCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JTable;
import javax.swing.ListCellRenderer;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
import pl.edu.utp.kursy.database.Academy;
import pl.edu.utp.kursy.database.Accomodation;
import pl.edu.utp.kursy.database.Company;
import pl.edu.utp.kursy.database.Course;
import pl.edu.utp.kursy.database.CourseEvent;
import pl.edu.utp.kursy.database.CourseEventCompanyAccomodation;
import pl.edu.utp.kursy.database.DatabaseManager;
import pl.edu.utp.kursy.database.Worker;


public class CourseEventWindow extends JFrameGUI implements CourseEventCompanyListener{

    public enum CourseEventWindowMode {

        ADD, EDIT
    };
    
    private Course course;
    private CourseEvent event;
    private Academy academy;
    private CourseEventWindowMode mode;
    private CourseEventWindowListener listener;
    private DatabaseManager dataManager;
    private String dateFormat = "dd/MM/yyyy";
    
    private ArrayList<Company> selectedCompanies;
    private ArrayList<Worker> selectedWorkers;

    public CourseEventWindow(CourseEvent event, Course course, Academy academy, CourseEventWindowListener listener) {
        initComponents();
        setCenteredPosition();
        this.listener = listener;
        this.event = event;
        this.course = course;
        this.academy = academy;

        dataManager = new DatabaseManager();
        selectedCompanies = new ArrayList<>();
        selectedWorkers = new ArrayList<>();

        if (event != null) {
            mode = CourseEventWindowMode.EDIT;
            setTitle(getString("edit_course_event"));
            approveButton.setText(getString("edit_course_event_button"));
            deleteButton.setVisible(true);
        } else {
            mode = CourseEventWindowMode.ADD;
            deleteButton.setVisible(false);
        }

        setDateChoosersFormat(dateFormat);
        setComponentsValues();
        initCompaniesTable();
        initWorkersList();
        refreshWorkersList();
    }

    public final void setDateChoosersFormat(String format) {
        endDateChooser.setDateFormatString(format);
        startDateChooser.setDateFormatString(format);
    }

    private void setComponentsValues() {
        if (event != null) {
            startDateChooser.setDate(event.getStartDate());
            endDateChooser.setDate(event.getEndDate());
            commentsTextArea.setText(event.getComments());
            vatSpinner.setValue(event.getVat());
            priceTextField.setText(String.valueOf(event.getCost()));
        }else{
            endDateChooser.setCalendar(Calendar.getInstance());
            startDateChooser.setCalendar(Calendar.getInstance());
            if(academy != null){
                vatSpinner.setValue(academy.getVat());
            }
            priceTextField.setText(String.valueOf(course.getCost()));
        }
    }

    private CourseEvent getEvent() {
        if (event == null) {
            event = new CourseEvent();
        }
        event.setStartDate(startDateChooser.getDate());
        event.setEndDate(endDateChooser.getDate());
        event.setComments(commentsTextArea.getText());
        event.setCost(Double.parseDouble(priceTextField.getText()));
        event.setVat((int)vatSpinner.getValue());
        
        Set<Company> eventCompanies = new HashSet(selectedCompanies);
        Set<Worker> eventWorkers = new HashSet(selectedWorkers);
        
        event.setCompanies(eventCompanies);
        event.setWorkers(eventWorkers);
        event.setCourse(course);

        return event;
    }
    
    private void initCompaniesTable(){
        ArrayList<Company> companies = dataManager.getAllCompanies();
        if(event != null){
            selectedCompanies.addAll(event.getCompanies());
        }
        ArrayList<CourseEventCompany> companiesItems = new ArrayList<>();
        for(Company company: companies){
            CourseEventCompany item = new CourseEventCompany(false, company, createDefautAccomodation());
            
            if(event != null){
                for(CourseEventCompanyAccomodation ceca :  event.getCourseEventsCompanyAccomodation()){
                    if(ceca.getCompany().getId() == company.getId()){
                        item.setAccomodation(ceca.getAccomodation());
                    }
                }
            }
            
            for(Company sCompany: selectedCompanies){
                if(company.getId() == sCompany.getId()){
                    item.setSelected(true);
                    break;
                }
            }
            companiesItems.add(item);
        }
        CompaniesTableModel model = new CompaniesTableModel(companiesItems);
        model.setCourseEventCompanyListener(this);
        companiesTable.setModel(model);
        initAccomodationChooser();
        setColumnSizes();
    }
    
    private void initAccomodationChooser(){
        TableColumn accomodationColumn = companiesTable.getColumnModel().getColumn(2);
        JComboBox<Accomodation> comboBox = new JComboBox<>();
        
        ArrayList<Accomodation> accomodations = dataManager.getAllAccomodations();
        
        comboBox.addItem(createDefautAccomodation());
        for(Accomodation a: accomodations){
            comboBox.addItem(a);
        }
        
        accomodationColumn.setCellEditor(new DefaultCellEditor(comboBox));
        accomodationColumn.setCellRenderer(new DefaultTableCellRenderer());
    }
    
    private Accomodation createDefautAccomodation(){
        Accomodation noAccomodation = new Accomodation();
        noAccomodation.setId(0);
        noAccomodation.setName(getString("no_accomodation"));
        return noAccomodation;
    }
    
    private void setColumnSizes(){
        companiesTable.getColumnModel().getColumn(0).setPreferredWidth(50);
        companiesTable.getColumnModel().getColumn(1).setPreferredWidth(160);
        companiesTable.getColumnModel().getColumn(2).setPreferredWidth(240);
        companiesTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
    }
    
    @Override
    public void onCourseEventCompanyChange(CourseEventCompany company) {
        if(company.isSelected()){
            selectedCompanies.add(company.getCompany());
        }else{
            selectedCompanies.remove(company.getCompany());
            removeCompanyWorkersSelection(company.getCompany());
        }
        refreshWorkersList();
    }
    
    private void removeCompanyWorkersSelection(Company company){
        ArrayList<Worker> workersToRemove = new ArrayList<>();
        for(Worker worker: company.getWorkers()){
            for(Worker sWorker: selectedWorkers){
                if(worker.getId() == sWorker.getId()){
                    workersToRemove.add(sWorker);
                }
            }
        }
        for(Worker worker: workersToRemove){
            selectedWorkers.remove(worker);
        }
    }

    private void initWorkersList() {
        workersList.setCellRenderer(new CheckListRenderer());
        if(event != null){
            selectedWorkers.addAll(event.getWorkers());
        }
        workersList.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                JList list = (JList) e.getSource();
                int index = list.locationToIndex(e.getPoint());
                if(index > -1){
                    CheckListItem<Worker> item = (CheckListItem<Worker>)list.getModel().getElementAt(index);
                    item.setSelected(! item.isSelected());
                    list.repaint(list.getCellBounds(index, index));

                    if(item.isSelected){
                        selectedWorkers.add(item.getObject());
                    }else{
                        selectedWorkers.remove(item.getObject());
                    }
                }
                
                refreshWorkersList();
            }
        });
    }
    
    private void refreshWorkersList(){
        ArrayList<Worker> workers = new ArrayList<>();
        for(Company company: selectedCompanies){
            workers.addAll(company.getWorkers());
        }
        
        ArrayList<CheckListItem<Worker>> workersItems = new ArrayList<>();
        
        for(Worker worker: workers){
            CheckListItem<Worker> item = new CheckListItem<>(worker);
            for(Worker sWorker: selectedWorkers){
                if(worker.getId() == sWorker.getId()){
                    item.setSelected(true);
                    break;
                }
            }
            workersItems.add(item);
        }
        workersList.setListData(workersItems.toArray());
        workersList.repaint();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        datesAndComentsPanel = new javax.swing.JPanel();
        endDateChooser = new com.toedter.calendar.JDateChooser();
        startDateChooser = new com.toedter.calendar.JDateChooser();
        startDateLabel = new javax.swing.JLabel();
        endDateLabel = new javax.swing.JLabel();
        commentsScrollPanel = new javax.swing.JScrollPane();
        commentsTextArea = new javax.swing.JTextArea();
        companiesAndWorkersPanel = new javax.swing.JPanel();
        workersScrollPanel = new javax.swing.JScrollPane();
        workersList = new javax.swing.JList();
        companiesScrollPanel = new javax.swing.JScrollPane();
        companiesTable = new javax.swing.JTable();
        buttonPanel = new javax.swing.JPanel();
        approveButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        priceLabel = new javax.swing.JLabel();
        priceTextField = new javax.swing.JTextField();
        vatLabel = new javax.swing.JLabel();
        vatSpinner = new javax.swing.JSpinner();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(getString("new_course_event"));
        setResizable(false);

        startDateLabel.setText(getString("course_start_date"));

        endDateLabel.setText(getString("course_end_date"));

        commentsScrollPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(getString("course_comments")));

        commentsTextArea.setColumns(20);
        commentsTextArea.setRows(5);
        commentsScrollPanel.setViewportView(commentsTextArea);

        javax.swing.GroupLayout datesAndComentsPanelLayout = new javax.swing.GroupLayout(datesAndComentsPanel);
        datesAndComentsPanel.setLayout(datesAndComentsPanelLayout);
        datesAndComentsPanelLayout.setHorizontalGroup(
            datesAndComentsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(datesAndComentsPanelLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(datesAndComentsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(datesAndComentsPanelLayout.createSequentialGroup()
                        .addComponent(startDateLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(startDateChooser, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(datesAndComentsPanelLayout.createSequentialGroup()
                        .addComponent(endDateLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(endDateChooser, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(commentsScrollPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        datesAndComentsPanelLayout.setVerticalGroup(
            datesAndComentsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(datesAndComentsPanelLayout.createSequentialGroup()
                .addGroup(datesAndComentsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(datesAndComentsPanelLayout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addGroup(datesAndComentsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(startDateChooser, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE)
                            .addComponent(startDateLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(datesAndComentsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(endDateChooser, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE)
                            .addComponent(endDateLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(datesAndComentsPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(commentsScrollPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        workersScrollPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(getString("workers")));

        workersList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        workersScrollPanel.setViewportView(workersList);

        companiesTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        companiesTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        companiesScrollPanel.setViewportView(companiesTable);

        javax.swing.GroupLayout companiesAndWorkersPanelLayout = new javax.swing.GroupLayout(companiesAndWorkersPanel);
        companiesAndWorkersPanel.setLayout(companiesAndWorkersPanelLayout);
        companiesAndWorkersPanelLayout.setHorizontalGroup(
            companiesAndWorkersPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, companiesAndWorkersPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(companiesScrollPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(workersScrollPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 278, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11))
        );
        companiesAndWorkersPanelLayout.setVerticalGroup(
            companiesAndWorkersPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(workersScrollPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, companiesAndWorkersPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(companiesScrollPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
        );

        buttonPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        approveButton.setText(getString("add_course_event_button"));
        approveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                approveButtonActionPerformed(evt);
            }
        });
        buttonPanel.add(approveButton);

        deleteButton.setText(getString("delete_course_event_button"));
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });
        buttonPanel.add(deleteButton);

        cancelButton.setText(getString("cancel"));
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });
        buttonPanel.add(cancelButton);

        priceLabel.setText(getString("price"));

        vatLabel.setText(getString("vat"));

        vatSpinner.setModel(new javax.swing.SpinnerNumberModel(23, 0, 100, 1));

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(datesAndComentsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addGap(198, 198, 198)
                                .addComponent(buttonPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addContainerGap())))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(priceLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(priceTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(vatLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(vatSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))))
            .addComponent(companiesAndWorkersPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addComponent(datesAndComentsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(priceLabel)
                    .addComponent(priceTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(vatLabel)
                    .addComponent(vatSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(companiesAndWorkersPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 617, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void approveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_approveButtonActionPerformed
        CourseEvent ev = getEvent();
        CompaniesTableModel model = (CompaniesTableModel)companiesTable.getModel();
        Set<CourseEventCompanyAccomodation> cecas = new HashSet<>();
        for(CourseEventCompany c: model.getCourseEventCompanies()){
            CourseEventCompanyAccomodation ceca = new CourseEventCompanyAccomodation();
            ceca.setEvent(ev);
            ceca.setAccomodation(c.getAccomodation());
            ceca.setCompany(c.getCompany());
            if(c.getAccomodation().getId() > 0){
                cecas.add(ceca);
            }
        }
        ev.setCourseEventsCompanyAccomodation(cecas);
        
        if (listener != null) {
            listener.onCourseEventWindowApprove(ev, mode);
        }
        dispose();
    }//GEN-LAST:event_approveButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
        if (listener != null) {
            listener.onCourseEventWindowDelete(getEvent());
        }
        dispose();
    }//GEN-LAST:event_deleteButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton approveButton;
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JButton cancelButton;
    private javax.swing.JScrollPane commentsScrollPanel;
    private javax.swing.JTextArea commentsTextArea;
    private javax.swing.JPanel companiesAndWorkersPanel;
    private javax.swing.JScrollPane companiesScrollPanel;
    private javax.swing.JTable companiesTable;
    private javax.swing.JPanel datesAndComentsPanel;
    private javax.swing.JButton deleteButton;
    private com.toedter.calendar.JDateChooser endDateChooser;
    private javax.swing.JLabel endDateLabel;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JLabel priceLabel;
    private javax.swing.JTextField priceTextField;
    private com.toedter.calendar.JDateChooser startDateChooser;
    private javax.swing.JLabel startDateLabel;
    private javax.swing.JLabel vatLabel;
    private javax.swing.JSpinner vatSpinner;
    private javax.swing.JList workersList;
    private javax.swing.JScrollPane workersScrollPanel;
    // End of variables declaration//GEN-END:variables

    public interface CourseEventWindowListener {
        public void onCourseEventWindowApprove(CourseEvent event, CourseEventWindowMode mode);
        public void onCourseEventWindowDelete(CourseEvent event);
    }
    


    private class CheckListItem<T> {

        private T object;
        private boolean isSelected = false;

        public CheckListItem(T object) {
            this.object = object;
        }
        
        public T getObject(){
            return object;
        }

        public boolean isSelected() {
            return isSelected;
        }

        public void setSelected(boolean isSelected) {
            this.isSelected = isSelected;
        }

        @Override
        public String toString() {
            return object.toString();
        }
    }

    private class CheckListRenderer extends JCheckBox implements ListCellRenderer {

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean hasFocus) {
            setEnabled(list.isEnabled());
            setSelected(((CheckListItem) value).isSelected());
            setFont(list.getFont());
            setBackground(list.getBackground());
            setForeground(list.getForeground());
            setText(value.toString());
            return this;
        }
    }
    
    public class CourseEventCompany{
        
        private boolean selected;
        private Company company;
        private Accomodation accomodation;
        
        public CourseEventCompany(boolean isSelected, Company company, Accomodation accomodation){
            this.selected = isSelected;
            this.company = company;
            this.accomodation = accomodation;
        }

        public boolean isSelected() {
            return selected;
        }

        public void setSelected(boolean isSelected) {
            this.selected = isSelected;
        }

        public Company getCompany() {
            return company;
        }

        public void setCompany(Company company) {
            this.company = company;
        }

        public Accomodation getAccomodation() {
            return accomodation;
        }

        public void setAccomodation(Accomodation accomodation) {
            this.accomodation = accomodation;
        }
    }
    
    private class CompaniesTableModel extends AbstractTableModel{
        
        private ArrayList<CourseEventCompany> companies;
        private CourseEventCompanyListener listener;
        
        private String columns[] = new String[]{
            "",
            getString("company"),
            getString("accomodation")
        };
        
        
        public CompaniesTableModel(ArrayList<CourseEventCompany> companies){
            this.companies = companies;
        }

        @Override
        public int getRowCount() {
            return companies.size();
        }

        @Override
        public int getColumnCount() {
            return columns.length;
        }
        
        @Override
        public String getColumnName(int column) {
            return columns[column];
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            return getValueAt(0, columnIndex).getClass();
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            if(columnIndex == 1){
                return false;
            }
            return true;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            
            switch(columnIndex){
                case 0:
                    return companies.get(rowIndex).isSelected();
                case 1:
                    return companies.get(rowIndex).getCompany().getName();
                case 2:
                    return companies.get(rowIndex).getAccomodation();
            }
            return "";
        }

        @Override
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
            
            switch(columnIndex){
                case 0:
                    companies.get(rowIndex).setSelected((boolean)aValue);
                    break;
                case 2:
                    companies.get(rowIndex).setAccomodation((Accomodation)aValue);
            }
            fireTableCellUpdated(rowIndex, columnIndex);
            
            if(columnIndex == 0 && listener != null){
                listener.onCourseEventCompanyChange(companies.get(rowIndex));
            }
            
        }

        public void setCourseEventCompanyListener(CourseEventCompanyListener listener) {
            this.listener = listener;
        }

        public ArrayList<CourseEventCompany> getCourseEventCompanies() {
            return companies;
        }
        
    }
}
interface CourseEventCompanyListener{
    public void onCourseEventCompanyChange(CourseEventWindow.CourseEventCompany company);
}